package com.csaba.swing.gui.util;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

@SuppressWarnings( "serial" )
public class MaxLengthDocument extends PlainDocument
{

	/**
	* Maximum length of the text
	*/
	private final int maxLength;

	/**
	* Default constructor.
	*
	* @param maxLength
	* the maximum number of characters that can be entered in the
	* field
	*/
	public MaxLengthDocument( final int maxLength )
	{
		super();
		this.maxLength = maxLength;
	}

	@Override
	public void insertString( final int offset, final String str, final AttributeSet attr ) throws BadLocationException
	{
		if ( getLength() + str.length() > maxLength )
		{
			return;
		}
		super.insertString(offset, str, attr);
	}

}